package com.lcx.security.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class StreamConfig implements InitializingBean {

    protected static Logger logger = LoggerFactory.getLogger(StreamConfig.class);

    @Value("${rtsp.username}")
    private String username;

    @Value("${rtsp.password}")
    private String password;

    @Value("${rtsp.ip}")
    private String ip;

    @Value("${rtsp.port}")
    private Integer port;

    @Value("${rtsp.suffix}")
    private String suffix;

    @Value("${rtmp.ip}")
    private String rtmp_ip;

    @Value("${rtmp.port}")
    private Integer rtmp_port;

    @Value("${rtmp.suffix}")
    private String rtmp_suffix;

    @Value("${hls.ip}")
    private String hls_ip;

    @Value("${hls.port}")
    private Integer hls_port;

    @Value("${hls.suffix}")
    private String hls_suffix;

    @Value("${hive.ip}")
    private String hive_ip;

    @Value("${hive.port}")
    private Integer hive_port;

    @Value("${ffmpeg.path}")
    private String ffmpeg_path;

    @Value("${hive.suffix}")
    private String hive_suffix;

    @Value("${file.path}")
    private String file_path;

    public static String USERNAME;
    public static String PASSWORD;
    public static String IP;
    public static Integer PORT;
    public static String URL;

    public static String RTMP_IP;
    public static Integer RTMP_PORT;
    public static String RTMP_URL;

    public static String HLS_IP;
    public static Integer HLS_PORT;
    public static String HLS_URL;

    public static String HIVE_IP;
    public static Integer HIVE_PORT;
    public static String HIVE_URL;

    public static String FILE_PATH;
    public static String FFMPEG_PATH;


    @Override
    public void afterPropertiesSet(){
        try {
            USERNAME = username;
            PASSWORD = password;
            IP = ip;
            PORT = port;
            if (!USERNAME.trim().equals("")) {
                URL = new StringBuilder("rtsp://").append(USERNAME).append(":").append(PASSWORD).append("@").append(IP).append(":")
                        .append(PORT).append(suffix).toString();
            } else {
                URL = new StringBuilder("rtsp://").append(IP).append(":")
                        .append(PORT).append(suffix).toString();
            }
        }catch (Exception e){
            logger.error("rtsp属性绑定错误");
            throw e;
        }

        try{
            RTMP_IP = rtmp_ip;
            RTMP_PORT = rtmp_port;
            RTMP_URL = new StringBuilder("rtmp://").append(RTMP_IP).append(":").append(RTMP_PORT).append(rtmp_suffix).toString();

            HLS_IP = hls_ip;
            HLS_PORT = hls_port;
            HLS_URL = new StringBuilder("rtmp://").append(hls_ip).append(":").append(hls_port).append(hls_suffix).toString();

            HIVE_IP = hive_ip;
            HIVE_PORT = hive_port;
            HIVE_URL = new StringBuilder("http://").append(hive_ip).append(":").append(hive_port).append(hive_suffix).toString();

            FFMPEG_PATH = ffmpeg_path;
            FILE_PATH = file_path;
        }catch (Exception e){
            logger.error("rtmp或hls属性绑定错误");
            throw e;
        }
    }
}
